import { Request, RequestHandler, Response } from 'express';
import {
  IGetDepartmentByIdReq,
  IAddDepartmentReq,
  IUpdateDepartmentReq,
  IDeleteDepartmentReq
} from '../../models/department.model';
import * as DepartmentService from '../services/department.service';

/**
 * Get active department records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getDepartments: RequestHandler = async (req: Request, res: Response) => {
  try {
    const departments = await DepartmentService.getDepartments();

    res.status(200).json({
      data: departments,
      code: 2000,
    })
  } catch (error) {
    console.error('[departments.controller][getDepartments][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取部门信息列表失败！'
    });
  }
};

/**
 * Get department record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getDepartmentById: RequestHandler = async (req: IGetDepartmentByIdReq, res: Response) => {
  try {
    const department = await DepartmentService.getDepartmentById(req.params.id);

    res.status(200).json({
      data: department,
      code: 2000,
    })
  } catch (error) {
    console.error('[departments.controller][getDepartmentById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取部门信息失败！'
    });
  }
};

/**
 * Inserts a new department record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addDepartment: RequestHandler = async (req: IAddDepartmentReq, res: Response) => {
  try {
    const result = await DepartmentService.insertDepartment(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[departments.controller][addDepartment][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '添加部门信息列表失败！'
    });
  }
};

/**
 * Updates existing department record
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const updateDepartmentById: RequestHandler = async (req: IUpdateDepartmentReq, res: Response) => {
  try {
    // const newFields = `
    //   departmentName = '${req.body.departmentName}';
    //   departmentDescription: string;
    //   departmentPhoneNumber: string;
    //   `
    const result = await DepartmentService.updateDepartment({...req.body, id: req.params.id});

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[departments.controller][updateDepartmentById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '更新部门信息列表失败！'
    });
  }
};

/**
 * deletes a department
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteDepartmentById: RequestHandler = async (req: IDeleteDepartmentReq, res: Response) => {
  try {
    const result = await DepartmentService.deleteDepartment(req.params.id);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[departments.controller][deleteDepartmentById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '删除部门信息列表失败！'
    });
  }
};